package com.wbb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbb.mapper.TbBrandMapper;
import com.wbb.mapper.TbSpecificationMapper;
import com.wbb.mapper.TbSpecificationOptionMapper;
import com.wbb.model.TbSpecification;
import com.wbb.model.TbSpecificationExample;
import com.wbb.model.TbSpecificationOption;
import com.wbb.model.TbSpecificationOptionExample;
import com.wbb.service.SpecService;
import com.wbb.util.pageList;
import com.wbb.util.queryParams;
import com.wbb.vo.SpecificationOptionVo;
import com.wbb.vo.SpecificationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {
    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;
// 分页+条件查询
    @Override
    public pageList<SpecificationVo> queryPage(queryParams<SpecificationVo> params) {
        Page<TbSpecification> page = PageHelper.startPage(params.getPageNum(), params.getPageSize());
        TbSpecificationExample example = new TbSpecificationExample();
        if (params.getParams().getSpecName() != null && !params.getParams().getSpecName().isEmpty()){
            example.createCriteria().andSpecNameLike("%"+params.getParams().getSpecName()+"%");
        }
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(example);
        PageInfo<TbSpecification> pageInfo = page.toPageInfo();
        List<TbSpecification> specifications = pageInfo.getList();
        List<SpecificationVo> collect = specifications.stream().map(spec -> {
            SpecificationVo vo = new SpecificationVo();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return new pageList<>(pageInfo.getTotal(),collect);
    }
// 删除表信息
    @Override
    public void deleteById(Long id) {
        // 删除Spec表
        specificationMapper.deleteByPrimaryKey(id);
        // 再删除SpecOption表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
    }
// 增加表信息
    @Override
    public void addSpec(SpecificationVo specVo) {
      // 先增加Spec表
        TbSpecification specification = new TbSpecification();
        BeanUtils.copyProperties(specVo,specification);
        specificationMapper.insertSpec(specification);
        Long id = specification.getId();
      // 再增加SpecOption表
        List<SpecificationOptionVo> optionVos = specVo.getOptionVos();
        List<TbSpecificationOption> collect = optionVos.stream().map(specOption -> {
            TbSpecificationOption specificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(specOption, specificationOption);
            specificationOption.setSpecId(id);
            return specificationOption;
        }).collect(Collectors.toList());
        specificationOptionMapper.batchInsert(collect);
    }
// 根据id查询表数据
    @Override
    public SpecificationVo selectSpecByOptionId(Long id) {
      // 查询spec表
        TbSpecification specification = specificationMapper.selectByPrimaryKey(id);
        SpecificationVo specificationVo = new SpecificationVo();
        BeanUtils.copyProperties(specification,specificationVo);
      // 再查询specOption表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = specificationOptionMapper.selectByExample(example);
        List<SpecificationOptionVo> optionVos = tbSpecificationOptions.stream().map(spec -> {
            SpecificationOptionVo specificationOptionVo = new SpecificationOptionVo();
            BeanUtils.copyProperties(spec, specificationOptionVo);
            return specificationOptionVo;
        }).collect(Collectors.toList());
        specificationVo.setOptionVos(optionVos);
        return specificationVo;
    }
// 修改信息
    @Override
    public void updateSpec(SpecificationVo specVo) {
      // 修改第一张表
        TbSpecification specification = new TbSpecification();
        BeanUtils.copyProperties(specVo,specification);
        specificationMapper.updateByPrimaryKeySelective(specification);
      // 修改第二张表, 先删除, 在增加
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(specVo.getId());
        specificationOptionMapper.deleteByExample(example);
      // 增加
        List<SpecificationOptionVo> optionVos = specVo.getOptionVos();
        List<TbSpecificationOption> collect = optionVos.stream().map(specOption -> {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(specOption, tbSpecificationOption);
            tbSpecificationOption.setSpecId(specVo.getId());
            return tbSpecificationOption;
        }).collect(Collectors.toList());
      // 批量添加
        specificationOptionMapper.batchInsert(collect);
    }
// 批量删除
    @Override
    public void batchSpec(List<Long> ids) {
      // 删除spec表的数据
        TbSpecificationExample example = new TbSpecificationExample();
        example.createCriteria().andIdIn(ids);
        specificationMapper.deleteByExample(example);
      // 再删除specOption表中的数据
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        optionExample.createCriteria().andSpecIdIn(ids);
        specificationOptionMapper.selectByExample(optionExample);
    }

    @Override
    public List<SpecificationVo> getAll() {
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(null);
        return tbSpecifications.stream().map(tbSpecification -> {
            SpecificationVo specificationVo = new SpecificationVo();
            BeanUtils.copyProperties(tbSpecification,specificationVo);
            return specificationVo;
        }).collect(Collectors.toList());
    }
}
